import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/bean/list_calendar_detail_entity.dart';
import 'package:flutter_app/network/Api.dart';
import 'package:flutter_app/network/ResultData.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:flutter_app/widget/CustomCalendar.dart';
import 'package:get/get.dart';

import 'duty_dialog_state.dart';

class DutyDialogLogic extends GetxController {
  final DutyDialogState state = DutyDialogState();

  final GlobalKey<CustomCalendarState> customCalendarKey = new GlobalKey();

  @override
  void onReady() {
    super.onReady();

    postListCalendarDetail(state.collectId);

  }
  /// 初始化数据
  void initData() {
    state.currentMonth = state.listCalendarDetailEntity![0].calendarOfMonth!;
    state.focusedDay = DateTime(int.parse(state.listCalendarDetailEntity![0].calendarOfYear!), int.parse(state.listCalendarDetailEntity![0].calendarOfMonth!), 1);
    state.firstDay = state.focusedDay;
    state.lastDay = DateTime(int.parse(state.listCalendarDetailEntity![0].calendarOfYear!), int.parse(state.listCalendarDetailEntity![0].calendarOfMonth!), 31);

    state.disabledDays = setDateTime(state.listCalendarDetailEntity!, "0");

    state.selectedDays = setDateTime(state.listCalendarDetailEntity!, "1");

    state.holidayDays = setHolidayDays(state.listCalendarDetailEntity!);

    /// 初始化 已选中的日期
    state.checkedTime.clear();
    state.selectedDays!.forEach((element) {
      state.checkedTime.add(element.year.toString() + "-" + element.month.toString() + "-" + element.day.toString());
    });
    state.selectedDays?.forEach((element) {
      customCalendarKey.currentState?.upDateSelectedDays(element.toString());
    });
  }

  ///
  /// type 类型 0禁用日期  1选中日期
  ///
  /// isWork      是否工作日 0不是1是
  /// signStatus	0未报名 1已报名
  /// isSelf      是否为自己报名 0否 1是
  ///
  List<DateTime> setDateTime(List<ListCalendarDetailEntity> listEntity,  String type) {
    List<DateTime> dateTime = [];
    listEntity.forEach((entity) {
      switch (type) {
        case "0":
          // 禁用日期
          if (entity.isWork == "0" || (entity.signStatus == "1" && entity.isSelf == "0")) {
          // if (entity.isWork == "0" || entity.signStatus == "1") {
            dateTime.add(DateTime(int.parse(entity.calendarOfYear!), int.parse(entity.calendarOfMonth!), int.parse(entity.calendarOfDay!)));
          }
          break;
        case "1":
          // 选中日期
          // if (entity.isWork == "1" && entity.signStatus == "1" && entity.isSelf == "1") {
          if (entity.isWork == "1" && entity.isSelf == "1") {
            dateTime.add(DateTime(int.parse(entity.calendarOfYear!), int.parse(entity.calendarOfMonth!), int.parse(entity.calendarOfDay!)));
          }
          break;
        default:
      }
    });
    return dateTime;
  }

  /// 获取假期日期（此处用于 标记已经报名1天的日期）
  /// scheduleList 报名记录  大于等于1时
  List<DateTime> setHolidayDays(List<ListCalendarDetailEntity> listEntity) {
    List<DateTime> dateTime = [];
    listEntity.forEach((entity) {
      if ((entity.scheduleList?.length ?? 0) >= 1) {
        dateTime.add(DateTime(int.parse(entity.calendarOfYear!), int.parse(entity.calendarOfMonth!), int.parse(entity.calendarOfDay!)));
      }
    });
    return dateTime;
  }


  /// 获取值班日历详情
  Future<void> postListCalendarDetail(String id) async {
    Map<String, dynamic> param = {'id': id};
    ResultData resultData = await Api.postListCalendarDetail(param);
    if (resultData.isSuccess) {
      List<dynamic> list = jsonDecode(jsonEncode(resultData.data['data']));
      state.listCalendarDetailEntity = list.map((e) => ListCalendarDetailEntity.fromJson(e)).toList();
      initData();
      refresh();
    } else {
      toast.show(resultData.data["message"]);
    }
  }

  /// 窗口值班报名
  Future<bool> postGoScheduleSign(String id, List<String> list) async {
    Map<String, dynamic> param = {
      "collectId": id,
      "type": "1",
      "workDateList": list
    };
    ResultData resultData = await Api.postGoScheduleSign(param);

    if (resultData.isSuccess) {
      return true;
    } else {
      toast.show(resultData.data["message"]);
      return false;
    }
  }

  @override
  void onClose() {
    super.onClose();
  }
}
